﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 部门
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        public DepartmentController(IDepartmentRepository departmentService, ILogRepository logRepository)
        {
            DepartmentService = departmentService;
            this.logRepository = logRepository;
        }

        public IDepartmentRepository DepartmentService { get; }

        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDepartment()
        {
            ApiResult apiResult = new ApiResult();
            var obj = DepartmentService.GetDepartment();
            if (obj != null)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功";
                apiResult.count = obj.Count;
                apiResult.data = obj;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "获取失败";
            return Ok(apiResult);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddDepartment(TDepartment dto)
        {        
            string uname = HttpContext.User.FindFirstValue("UserName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            dto.CreateUserId = uid;
            dto.CreateUserName= uname;
            dto.CreateDate = DateTime.Now;
            bool isko = DepartmentService.AddDepartment(dto);
            if (isko)
            {            
                string runame = HttpContext.User.FindFirstValue("RealName");          
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了部门" + dto.DepartmentName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！编码已存在！", "500"));
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateDepartment(TDepartment dto)
        {
            if(dto.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }           
            string uname = HttpContext.User.FindFirstValue("UserName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            dto.ModifyUserId= uid;
            dto.ModifyUserName= uname;
            dto.ModifyDate = DateTime.Now;
            bool isko = DepartmentService.UpdateDepartment(dto);
            if (isko)
            {          
                string runame = HttpContext.User.FindFirstValue("RealName");   
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了部门" + dto.DepartmentName;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！编码已存在！", "500"));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelDepartment(int ids)
        {
            if(ids<=0)
            {            
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool isko = DepartmentService.DelDepartment(ids);
            if (isko)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了部门";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }

        /// <summary>
        /// 部门单位类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTDepartmentDic()
        {
            return Ok(BaseResult.Result(DepartmentService.GetTDepartmentDic().Select(c => new { c.Id, c.ItemName }), "200"));
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetDepartmentst(int ids)
        {
            if (ids <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var result = DepartmentService.GetDepartment(ids);
            return Ok(BaseResult.Result(result, "查询成功", "200"));
        }
        /// <summary>
        /// 部门分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetDepartments(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totaCount = 0;
            var list = DepartmentService.GetDepartments(name, pageIndex, pageSize, out totaCount);
            return Ok(BaseResult.Result(list, "查询成功", "200", totaCount));
        }
    }
}
